<?php

/**
 * Created by PhpStorm.
 * User: d7
 * Date: 2016/1/29
 * Time: 11:36
 */
class Cache_multiple
{
    protected $cache;
    protected $prefix;
    function __construct($cache_name='redis',$prefix='')
    {
        $this->ci = &get_instance();
        $this->ci->load->driver('cache');
        $this->cache = $this->ci->cache->$cache_name;
        $this->prefix = $prefix;
    }

    function set_counter($name,$value,$expire,$func='increment'){
        if($num = $this->get($this->prefix.$name)){
            return $this->$func($name,$num+$value);
        }
        return $this->cache->save($this->prefix.$name,$value,$expire);
    }

    function set_string($name,$value,$expire){
        return $this->cache->save($this->prefix.$name,$value,$expire);
    }

    function get($name){
        return $this->cache->get($this->prefix.$name);
    }

    private function increment($name,$num){
        return $this->cache->increment($this->prefix.$name,$num);
    }

    private function decrement($name,$num){
        return $this->cache->decrement($this->prefix.$name,$num);
    }
}